<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 角色信息
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
            type="primary"
            icon="el-icon-delete"
            class="handle-del mr10"
            @click="delAllSelection"
        >删除</el-button>
        <el-input v-model='query.rolename'  placeholder="角色名" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="addRole">添加</el-button>
      </div>
      <el-table
          :data="tableData"
          border
          class="table"
          ref="multipleTable"
          header-cell-class-name="table-header"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="rid" label="角色编号" width="55" align="center"></el-table-column>
        <el-table-column prop="rolename" label="角色名"></el-table-column>
          <el-table-column prop="remark"  label="备注"></el-table-column>
          <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, JSON.parse(JSON.stringify(scope.row)))"
            >编辑
            </el-button>
            <el-button
                type="text"
                icon="el-icon-edit"
                class="red"
                @click="setPermission(scope.row)"
            >设置权限
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
<!--        // layout prev 左箭头 pager 页数 next 右箭头
        //@current-change 页数改变时间 有一个参数 就是当前页数
        //total 条数 会根据总条数和每页显示的条数进行 分页-->
        <el-pagination
            background
            layout="prev, pager, next"
            @current-change="preNext"
            :page-size='pagesize'
            :total="total">
        </el-pagination>
      </div>
    </div>
<!--权限弹出框-->
      <el-dialog title='设置权限' :visible='editVisible1' width='30%'>
<!--      default-expanded-keys 默认展开
           default-checked-keys 默认选中
            :default-expanded-keys=""
              :default-checked-keys=""  -->
          <el-tree
              ref='tree'
              :data="data"
              show-checkbox
              :default-expanded-keys='myPermission'
              :default-checked-keys="myPermission"
              node-key="id"
              :props="defaultProps"
              >
          </el-tree>
          <el-button @click='reset'>取消 </el-button>
          <el-button @click='submit1'>确认</el-button>

      </el-dialog>
    <!-- 编辑弹出框 -->
    <el-dialog :title='title' :visible.sync='editVisible' width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="角色名">
          <el-input v-model="form.rolename"></el-input>
        </el-form-item>
          <el-form-item label="备注">
              <el-input v-model="form.remark"></el-input>
          </el-form-item>
      </el-form>
      <template #footer>
                <span class="dialog-footer">
                    <el-button @click="editVisible = false">取 消</el-button>
                    <el-button type="primary" @click="submit">确 定</el-button>
                </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "roletable",
  data() {
    return {
      query: {
        pageNum: 1,
        pageSize: 10
      },
      tableData: [],
      multipleSelection: [],
      delList: [],
      editVisible: false,
        editVisible1:false,
      total: 6, pagesize:5,
      form: {},
      idx: -1,
      id: -1,
        myPermission:[],
        title:[],
        rid:undefined,
        data: [{
            id: 'base',
            label: '基础管理',
            children: [
                {
                id: 'album',
                label: '专辑列表',
            },    {
                    id: 'emp',
                    label: '员工列表',
                },{
                    id: 'empadd',
                    label: '员工添加',
                },{
                    id: 'voice',
                    label: '音频添加',
                },{
                    id: 'voicetable',
                    label: '音频列表',
                },{
                    id: 'menu',
                    label: '菜单显示',
                }
                ,{
                    id: 'type',
                    label: '类型列表',
                }
            ]
        }, {
            id: 'table',
            label: '审批管理',
            children: [{
                id: 'approval',
                label: '专辑审批'
            }, {
                id: 'appdetail',
                label: '审批详情'
            }]
        },{
            id: 'permission',
            label: '权限管理',
            children: [{
                id: 'role',
                label: '角色列表'
            }]
        },{
            id: 'usermanger',
            label: '用户管理',
            children: [{
                id: 'user',
                label: '用户列表'
            }]
        },{
            id: 'ordermanger',
            label: '订单管理',
            children: [{
                id: 'vip',
                label: '订单列表'
            }]
        }],
        defaultProps: {
            children: 'children',
            label: 'label'
        }
    };
  },
  created() {
    this.getData();
  },
  methods: {
      //设置权限操作
      setPermission(row){
          this.myPermission=[]
          this.editVisible1=true;
          this.rid=row.rid;
          //查询拥有的权限
          this.$http({
              method:'post',
              url:'role/getPermission',
              params:{rid:this.rid}
          }).then((res)=>{
          this.myPermission=res

          })
      },//设置权限取消操作
      reset(){
         location.reload()
          this.editVisible1=false;
      },
      submit1() {
          let res = this.$refs.tree.getCheckedKeys()//树形菜单的获取选中数据的方法
          let str="";
          for (let i=0;i<res.length;i++){
              str+=res[i]+","
          }
          this.$http({
              method:'post',
              url:'role/setPermission',
              params:{rid:this.rid,str:str}
          }).then((res)=>{
              if(res!=false){
                  this.$message.success('设置成功')
                  this.editVisible1=false
              }else {
                  this.$message.success('设置失败')

              }
          })
          },
    getData() {
        //音频1列表
     this.$http({
       url:'http://localhost:8090/role/selectRole1',
       method:'post',
       params:{pageNum:1,pageSize:5,rolename:this.query.rolename}
     }).then(response=>{
         //console.log(response)
       //console.log(response)
       this.tableData=response.list
         this.total=response.total
         this.pagesize=response.pageSize
     })

    },
    // 触发搜索按钮
    handleSearch() {
      this.$http({
        url:'http://localhost:8090/role/selectRole1',
        method:'post',
        params:{pageNum:1,pageSize:5,rolename:this.query.rolename}
      }).then(response=>{
          this.total=response.total
          this.pagesize=response.pageSize
        this.tableData=response.list
      })
    }
    ,
    // 删除操作
    handleDelete(index) {
      // 二次确认删除
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning"
      })
          .then(() => {
            this.$message.success("删除成功");
            this.tableData.splice(index, 1);
          })
          .catch(() => {});
    },
      // 多选操作
      handleSelectionChange(val) {
          this.multipleSelection = val;
      },
    delAllSelection() {
        let str="";
        for (let i=0;i<this.multipleSelection.length;i++){
            str+=this.multipleSelection[i].rid+","
        }
        this.$http({
            url:'/role/delRole',
            method:'post',
            params:{str:str}
        }).then(res=>{
            if(res==true){
                this.$message.success('删除成功')
                this.getData()
            }else {
                this.$message.error('删除失败该角色不能删除')
            }

        })
    },
    // 编辑操作
    handleEdit(index, row) {
        //文件上传传递的参数
        this.title='编辑';
      this.idx = index;
      this.form = row;
      this.editVisible = true;
    },
    // 保存编辑
    saveEdit() {

    },addRole(){//添加操作
        this.editVisible=true;
        this.title='添加';
      },
      submit(){//动态提交
        if(this.form.rid==undefined){
            this.$http({
                method:'post',
                url:'role/roleAdd',
                params:{rolename:this.form.rolename,remark:this.form.remark}
            }).then((res)=>{
           if(res==true){
               this.$message.success('添加成功')
               this.getData()
               this.editVisible=false
           }else {
               this.$message.error('添加失败')
               this.editVisible=false
           }
            }
        )

        }else {
            this.$http({
                url:'http://localhost:8090/role/updRole',
                method:'post',
                params:{rolename:this.form.rolename,remark:this.form.remark,rid:this.form.rid
                }
            }).then(res=>{
                if(res===true){
                    this.$message.success("修改成功")
                    this.getData()
                    this.editVisible=false
                }else {
                    this.$message.warning("修改失败")
                    this.editVisible=false
                }
            })
            this.editVisible = false;
        }
      }
      ,
    // 分页导航
    handlePageChange() {
      this.getData();
    },preNext(val){
      this.$http({
        url:'http://localhost:8090/role/selectRole',
        method:'post',
        params:{pageNum:val,pageSize:5,rolename:this.query.rolename}
      }).then(response=>{
          this.total=response.total
          this.pagesize=response.pageSize
        this.tableData=response.list
      })
    }
  }
};
</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}
</style>
